package com.yingxin.yxerp.module.dingtalk.help;

import cn.hutool.core.collection.CollectionUtil;
import com.dingtalk.api.response.OapiSmartworkHrmEmployeeV2ListResponse;
import com.yingxin.yx.framework.commons.helper.DateHelper;
import com.yingxin.yxerp.module.dingtalk.entity.FieldTypeEnum;
import com.yingxin.yxerp.module.dingtalk.myinterface.DingUserFieldCode;
import com.yingxin.yxerp.module.dingtalk.vo.DingFieldValueVo;

import java.lang.reflect.Field;
import java.util.Date;
import java.util.List;

/**
 * 员工档案赋值工具类
 */

public class DingUserHelp {

    public static void reflectionUserInfo(Object obj, OapiSmartworkHrmEmployeeV2ListResponse.EmpFieldDataVo empFieldDataVo){
        // 获取Obj的所有属性
        Field[] fields = obj.getClass().getDeclaredFields();
        // 向对应的属性赋值
        for( Field field : fields ){
            // 设置属性可访问
            field.setAccessible( true );
            try {
                if( field.isAnnotationPresent( DingUserFieldCode.class ) ){
                    DingUserFieldCode dingUserFieldCode = field.getAnnotation( DingUserFieldCode.class );
                    if( empFieldDataVo.getFieldCode().trim().equals( dingUserFieldCode.fileCode().trim() ) ){
                        List<OapiSmartworkHrmEmployeeV2ListResponse.FieldValueVo> fieldValueVos = empFieldDataVo.getFieldValueList();
                        if( CollectionUtil.isNotEmpty( fieldValueVos ) ){
                            // 文本
                            if( dingUserFieldCode.fieldType().trim().equals( FieldTypeEnum.TextField.getType() ) ){
                                field.set( obj, fieldValueVos.get(0).getLabel() );
                            }
                            // 选项类型
                            if( dingUserFieldCode.fieldType().trim().equals( FieldTypeEnum.DDSelectField.getType() ) ){
                                DingFieldValueVo vo = new DingFieldValueVo();
                                vo.setItemIndex( fieldValueVos.get(0).getItemIndex() );
                                vo.setValue( fieldValueVos.get(0).getValue() );
                                vo.setLabel( fieldValueVos.get(0).getLabel() );
                                field.set( obj, vo );
                            }
                            // 日期类型
                            if( dingUserFieldCode.fieldType().trim().equals( FieldTypeEnum.DDDateField.getType() ) ){
                                try {
                                    Date date = DateHelper.stringToDate( fieldValueVos.get(0).getLabel() );
                                    field.set( obj, date );
                                }catch (Exception e){ }
                            }
                            // 长日期类型
                            if( dingUserFieldCode.fieldType().trim().equals( FieldTypeEnum.DDDateWithLongField.getType() ) ){
                                try {
                                    Date date = DateHelper.stringToDate( fieldValueVos.get(0).getLabel() );
                                    field.set( obj, date );
                                }catch (Exception e){ }
                            }
                            // 图片类型
                            if( dingUserFieldCode.fieldType().trim().equals( FieldTypeEnum.DDPhotoField.getType() ) ){
                                field.set( obj, fieldValueVos.get(0).getLabel() );
                            }
                            break;
                        }
                    }
                }
            } catch (IllegalAccessException e) {
                throw new RuntimeException(e);
            }
        }
    }
}
